

import SwiftUI

struct TransResultView: View {
    let id: Int64
    @Environment(\.presentationMode) var presentationMode
    @State private var beforeSelectedLanguage = "English"
    @State private var afterSelectedLanguage = "English"
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {

                ZStack {
                    MainView()
                    VStack {
                        Back(title: "Result", onBackTap: {
                            self.presentationMode.wrappedValue.dismiss()
                        }, onImageTap: {})
                       
                        VStack {
                            let translation = DatabaseManager.shared.getTranslation(byId: Int64(id))
                            ChoiceLanguage(beforeSelectedLanguage: $beforeSelectedLanguage, afterSelectedLanguage: $afterSelectedLanguage)
                                .onAppear {
                                    beforeSelectedLanguage = translation.beforeLanguage
                                    afterSelectedLanguage = translation.afterLanguage
                                   
                                }
                                .padding(.top, 20)
                            
                            RoundedRectangle(cornerRadius: 10)
                                .strokeBorder(Color.blue, lineWidth: 2)
                                .background(Color.clear) // 设置背景为透明
                                .frame(height: 300)
                                .overlay(
                                    VStack(spacing: 0) {
                                        TextView(text: translation.beforeText)
                                        
                                        Divider()
                                            .background(Color.gray)
                                            .padding(.horizontal, 10)
                                        
                                        TextView(text: translation.afterText)
                                    }
                                )
                                .overlay(
                                    BotoomView(id:id,text: translation.afterText, language: afterSelectedLanguage  )
                                
                                )
                                .padding()
                            
                            Spacer()
                        }
                    }
                }
            }
            .onAppear {
                print("in a translate layout get data id value is : \(id)")
            }
            .navigationViewStyle(StackNavigationViewStyle())

            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}



#Preview {
    TransResultView(id:-1)
}

struct ChoiceLanguage: View {
    @State private var isBefore = false
    @State private var isAfter = false
    @State private var isSwittch = false
    @Binding var beforeSelectedLanguage: String
    @Binding var afterSelectedLanguage: String
    var body: some View {
        ZStack{
           
               
            
            LinearGradient(gradient: Gradient(colors: [Color("alphaStart"), Color("alphaEnd")]),
                           startPoint: .leading,
                           endPoint: .trailing)
            .cornerRadius(20)
            .frame(height: 48)
            
            HStack{
                HStack {
                    Text("\(beforeSelectedLanguage)")
                        .font(/*@START_MENU_TOKEN@*/.headline/*@END_MENU_TOKEN@*/)
                        .fontWeight(.bold)
                    Image("choice")
                }.padding(.leading)
                    .onTapGesture {
                        self.isBefore.toggle()
                    }.sheet(isPresented: $isBefore, content: {
                        ChoiceLanguageView(selectedLanguage: $beforeSelectedLanguage, isPresented: $isBefore)
                        
                    })
                
                
                Spacer()
                Image("switch")
                Spacer()
                HStack {
                    Text("\(afterSelectedLanguage)")
                        .font(/*@START_MENU_TOKEN@*/.headline/*@END_MENU_TOKEN@*/)
                        .fontWeight(.bold)
                    Image("choice")
                }.padding(.trailing)
                    .onTapGesture {
                        self.isAfter.toggle()
                    }.sheet(isPresented: $isAfter, content: {
                        ChoiceLanguageView(selectedLanguage: $afterSelectedLanguage, isPresented: $isAfter)
                        
                    })
                
            }
        }    .padding(.horizontal,20)
            .padding(.top,10)
    }
}


struct BotoomView: View {
    var id:Int64
    var text :String
    var language:String
    @State var isFavorite = false
    var body: some View {
        VStack {
            Spacer()
            HStack {
                Spacer()
                HStack(spacing: 10) {
                    Image("voice1")
                        .resizable(resizingMode: .stretch)
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 25,height: 25)
                        .onTapGesture {
                            speak(text: text, language: getLanguageCode(for: language))
                        }
                    Spacer()
                    Image("copy1")
                        .resizable(resizingMode: .stretch)
                        .aspectRatio(contentMode: .fit)
                       
                        .frame(width: 25,height: 25)
                        .onTapGesture {
                            writeToClipboard(text: text)
                        }
                   
//                    Image("share").padding(.trailing,10).onTapGesture {
//                        shareText(text)
//                    }
                }
                .padding(5)
            }
        }.onAppear{
           
        }
    }
    func shareText(_ text: String) {
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                  let rootViewController = windowScene.windows.first?.rootViewController else {
                return
            }
            
            let activityViewController = UIActivityViewController(activityItems: [text], applicationActivities: nil)

            // 在 iPad 上设置 sourceView
            if let popoverController = activityViewController.popoverPresentationController {
                popoverController.sourceView = rootViewController.view
                popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
                popoverController.permittedArrowDirections = []
            }

            rootViewController.present(activityViewController, animated: true, completion: nil)
        }
}

struct TextView: View {
    var text = "声明"
    var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                HStack {
                    Text(text)
                        .padding(.top, 5)
                        .padding(.leading, 5)
                        .foregroundColor(.white)

                    Spacer()
                }
                Spacer()
            }
            .frame(height: 150)
        }
    }
}
